/*
 * test_session.cpp
 *
 *  Created on: Dec 14, 2013
 *      Author: killerg
 */

#include "share/session_mgr.h"
#include "share/sl_msg.pb.h"

#include <unistd.h>

struct client_session_t2 {
    tran_s* _trans;
    int64_t _sessid;
    double _idle_timer;

    client_session_t2(int64_t sessid_, tran_s* trans):
        _trans(trans), _sessid(sessid_) {
        _idle_timer = 10.0f;
    }

    void update();
    void on_connect();
    void on_disconnect();
};

struct server_sess : cli_session_t<sl::LMsg, sl::LMsgID_ARRAYSIZE> {
    typedef cli_session_t<sl::LMsg, sl::LMsgID_ARRAYSIZE> base;
    bool init();
};

svr_session_t<client_session_t2, sl::SMsg, sl::SMsgID_ARRAYSIZE, sl::LMsg, sl::LMsgID_ARRAYSIZE> ls;

void handle_smsg(client_session_t2* sess, sl::SMsg* msg) {
    printf("handle smsg");

    auto nmsg = new sl::LMsg;
    nmsg->set_id(sl::ID_LLogon);
    nmsg->mutable_logon()->set_ok(true);
    sess->_trans->send(nmsg);
}

void handle_lmsg(sl::LMsg* msg) {
    printf("handle lmsg");
}


bool server_sess::init() {
    base::reg(sl::ID_LLogon, handle_lmsg);
    base::reg(sl::ID_LPlayerWillLogon, handle_lmsg);
    return true;
//    return query_ip("killgxlin.vicp.cc", [this](const std::string &ip){
//        base::init(fmt_cstr("%s:9877", ip.c_str()), [this](){
//            auto nmsg = new sl::SMsg;
//            nmsg->set_id(sl::ID_SLogon);
//            nmsg->mutable_logon()->set_ip("1");
//            nmsg->mutable_logon()->set_port(2);
//            nmsg->mutable_logon()->set_serverid(0);
//            tran.send(nmsg);
//        });
//    });
}


void client_session_t2::update() {

}
void client_session_t2::on_connect() {
    printf("on connect \n");
}
void client_session_t2::on_disconnect() {
    printf("on disconnect \n");
}


server_sess ss;


bool test_session() {
    ls.init("0.0.0.0", 9877, 1);
    ls.reg(sl::ID_SLogon, handle_smsg);

    ss.init();

    while (true) {
        ls.update(0.2f);
        ss.update();
        sleep(1);
    }

    ss.destroy();
    ls.destroy();

    return true;
}

